import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:qaun_ni_dev/provider/real_name_provider.dart';
import 'package:qaun_ni_dev/pages/realNameCheck/real_detail.dart';


class RealNamePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('实名认证审核'),
      ),
      body: RealNameList(),
    );
  }
}


class RealNameList extends StatefulWidget {
  @override
  _RealNameListState createState() => _RealNameListState();
}

class _RealNameListState extends State<RealNameList> {
  GlobalKey<RefreshFooterState> _footerKeyReal = new GlobalKey<RefreshFooterState>();
  GlobalKey<RefreshHeaderState> _headerKeyReal = new GlobalKey<RefreshHeaderState>();

  void initState() {
    super.initState();
    _getList();
  }

  void _getList() {
    Provider.of<RealNameProvider>(context, listen: false).getRealNameList(context);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 620,
      padding: EdgeInsets.only(top: 10, left: 15, right: 15),
      child: Consumer<RealNameProvider>(
        builder: (context, data, _) {
          if (data.realNameListData.length > 0) {
            return Container(
              child: EasyRefresh(
                  refreshFooter: ClassicsFooter(
                      key: _footerKeyReal,
                      bgColor: Colors.white,
                      textColor: Colors.pink,
                      moreInfoColor: Colors.pink,
                      showMore: true,
                      noMoreText: '没有更多了',
                      moreInfo: '加载中',
                      loadReadyText: '上拉加载'),
                  refreshHeader: ClassicsHeader(
                      key: _headerKeyReal,
                      refreshText: "下拉刷新数据",
                      refreshReadyText: "准备刷新数据",
                      refreshingText: "数据刷新中...",
                      refreshedText: "刷新完成",
                      bgColor: Colors.pink,
                      textColor: Colors.white,
                      moreInfoColor: Colors.white,
                      refreshHeight: 70.0,
                      isFloat: false,
                      showMore: false,
                      moreInfo: "Updated at %T"),
                  child: ListView.builder(
                      itemCount: data.realNameListData.length,
                      itemBuilder: (context, index) {
                        return _listWidget(data.realNameListData, index);
                      }),
                  loadMore: () async {
                    print('加载更多的');
                    Provider.of<RealNameProvider>(context, listen: false).getRealNameMoreList(context);
                  },
                  onRefresh: () async {
                    print('上拉刷新');
                    Provider.of<RealNameProvider>(context, listen: false).getRealNameList(context);
                  }),
            );
          } else {
            return Text('请稍等，数据加载中或暂无数据');
          }
        },
      ),
    );
  }
  Widget _listWidget (List realList,int index) {
    return InkWell(
      onTap: () {
        Navigator.push(context, MaterialPageRoute(
            builder: (context) {
              return RealDetail(realList[index]);
            }
        ));
      },
      child: Container(
        margin: EdgeInsets.only(bottom: 10),
        decoration: BoxDecoration(
            border: Border.all(width: 1,color: Colors.black12),
            borderRadius: BorderRadius.all(Radius.circular(6))
        ),
        child: Row(
          children: <Widget>[
            _leftOrderNumber(realList,index),
            _rightContent(realList,index),
          ],
        ),
      ),
    );
  }

  Widget _leftOrderNumber(realList,index) {
    return Container(
      margin: EdgeInsets.only(right: 10),
      padding: EdgeInsets.only(left: 6),
      width: 50,
      child: Column(
        children: <Widget>[
          Text('序号${index}',
            textAlign: TextAlign.center,

          ),
        ],
      ),
    );
  }

  Widget _rightContent(realList,index) {
    return Container(
      padding: EdgeInsets.only(left: 8,right: 6,top: 6,bottom: 6),
      decoration: BoxDecoration(
          border: Border(
            left: BorderSide(color: Colors.grey,width: 1),
          )
      ),
      child: Column(
        children: <Widget>[
          Row(
            children: <Widget>[
              Container(
                width: 70,
                child: Text(
                  '用户id:',
                ),
              ),
              Container(
                width: 160,
                child: Text('${realList[index].id}'),
              ),
            ],
          ),
          Row(
            children: <Widget>[
              Container(
                width: 70,
                child: Text(
                  '用户昵称:',
                ),
              ),
              Container(
                width: 160,
                child: Text('${realList[index].nickName}'),
              ),
            ],
          ),
        ],
      ),
    );
  }
}

